package kr.co.casually;

import java.util.List;

import kr.co.casually.map.MyItemizedOverlay;
import kr.co.casually.util.MapUtil;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


public class LocationMapView extends MapActivity {
	private ProgressDialog progressDialog;
	final private int PROGRESS_DIALOG = 0;
	
	private MapUtil mapUtil;
	private Double lat =0.0;
	private Double lng =0.0;
	private String loc_name;
	private MapView mapView; // 맵뷰 객체
	private MapController mapController; // 맵을 줌시키거나, 이동시키는데 사용될 컨트롤러
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		mapView = (MapView) findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);
		mapView.setSatellite(false);
		mapController = mapView.getController();
		mapController.setZoom(18);
		mapView.setSatellite(false);
		threadStart();
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	
	private void mapMake(){
		mapUtil = new MapUtil();
		Bundle extra = getIntent().getExtras();
		loc_name = extra.getString("loc_name");
		String addr = extra.getString("addr");
		String enAddr = extra.getString("enAddr");
		
		lat = mapUtil.getGoogleLat(enAddr, "json");
		lng = mapUtil.getGooglelng(enAddr, "json");
        
		
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.bubble);
		MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable, this);
		
		
		GeoPoint geoPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
		OverlayItem overlayitem = new OverlayItem(geoPoint, loc_name, addr);

		
		
		itemizedoverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedoverlay);
		

		mapView.getOverlays().add(itemizedoverlay);
		mapView.getController().animateTo(geoPoint);
		mapView.postInvalidate();
		
	}
	
	
	private void threadStart() {
		showDialog(PROGRESS_DIALOG);
		new Thread() {
			public void run() {
				mapMake();
				Message msg = handler.obtainMessage();
				handler.sendMessage(msg);
			}
		}.start();
	}

	final Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			progressDialog.dismiss();
			LocationMapView.this.removeDialog(PROGRESS_DIALOG);
			

		}
	};
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case PROGRESS_DIALOG:

			progressDialog = new ProgressDialog(this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setMessage("Now loading..");
			progressDialog.setIndeterminate(true);
			progressDialog.setCancelable(false);
			return progressDialog;

		}
		return null;

	}

}